package com.ycsj.goldmedalnewconcept.fragment;

import java.util.ArrayList;
import java.util.List;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.squareup.picasso.Picasso;
import com.ycsj.goldmedalnewconcept.R;
import com.ycsj.goldmedalnewconcept.activity.MineActivity;
import com.ycsj.goldmedalnewconcept.constant.ConfigConstant;
import com.ycsj.goldmedalnewconcept.qrscan.CaptureActivity;
import com.ycsj.goldmedalnewconcept.utils.IndicatorManager;
import com.ycsj.goldmedalnewconcept.utils.IndicatorManager.onSelectedListener;
import com.ycsj.goldmedalnewconcept.utils.SPUtil;
import com.ycsj.goldmedalnewconcept.utils.SPUtils;
import com.ycsj.goldmedalnewconcept.view.magicindicator.MagicIndicator;

@SuppressLint("CutPasteId")
public class HomeFragment extends BaseFragment {

	public static final String TAG = "HomeFragment";

	public static volatile HomeFragment fragment;
	private Activity mActivity;
	private TextView mTitleTv;
	private ViewPager mViewPager;
	private TabPagerAdapter mPagerAdapter;
	// private TopIndicator mTopIndicator;
	private MagicIndicator mag;

	// 填充Vp的Fragments集合
	private List<Fragment> mFragments;

	// 顶部的Tab切换标题集合
	private String[] mTitles = { "单词", "检测", "语法", "跟读" };

	public static HomeFragment newInstance() {
		if (fragment == null) {
			synchronized (HomeFragment.class) {
				if (fragment == null) {
					fragment = new HomeFragment();
				}
			}
		}
		return fragment;
	}

	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
		this.mActivity = activity;
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.fragment_home, container, false);
		return view;
	}

	@Override
	public void onViewCreated(View view, Bundle savedInstanceState) {
		super.onViewCreated(view, savedInstanceState);

		// 初始化Framgents的方法
		initFragments();
		// 初始化方法
		initViews(view);
	}

	private void initFragments() {
		mFragments = new ArrayList<>();

		// 单词Fragment
		PreviewFragment mPreviewFragment = (PreviewFragment) Fragment
				.instantiate(mActivity, PreviewFragment.class.getName());
		// 检测Fragment
		DetectionFragment mDetectionFragment = (DetectionFragment) Fragment
				.instantiate(mActivity, DetectionFragment.class.getName());
		// 语法Fragment
		GrammarFragment mClassFragment = (GrammarFragment) Fragment
				.instantiate(mActivity, GrammarFragment.class.getName());

		// 跟读的Framgent
		FollowReadFragment followReadFragment = (FollowReadFragment) Fragment
				.instantiate(mActivity, FollowReadFragment.class.getName());

		mFragments.add(mPreviewFragment);
		mFragments.add(mDetectionFragment);
		mFragments.add(mClassFragment);
		mFragments.add(followReadFragment);
	}

	/**
	 * 初始化View的方法
	 * 
	 * @param view
	 */
	private void initViews(View view) {

		ImageView iv_menu = (ImageView) view.findViewById(R.id.iv_menu);
		ImageView iv_qrcode = (ImageView) view.findViewById(R.id.iv_qrcode);

		String icon = SPUtil.getString(getActivity(), "icon", "");
		if (!TextUtils.isEmpty(icon)) {
			Picasso.with(getActivity()).load(icon).into(iv_menu);
		}

		// 二维码扫描
		iv_qrcode.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent intent = new Intent(getActivity(), CaptureActivity.class);
				startActivity(intent);
			}
		});

		// 左边的菜单栏
		iv_menu.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// 未登录,跳转到登录界面
				// if (!SPUtils.getBoolean(getActivity(),
				// ConfigConstant.IS_LOGIN_ON)) {
				//
				// Intent intent = new Intent(getActivity(),
				// StartActivity.class);
				// startActivity(intent);
				//
				// } else {
				// 如果登录,跳转到个人资料界面
				Intent intent = new Intent(getActivity(), MineActivity.class);
				startActivity(intent);
				// }
			}
		});

		// 保存当前Fragment的TAG值
		SPUtils.put(getActivity(), ConfigConstant.CURRENTFRAGMENT, TAG);

		// ViewPager
		mViewPager = (ViewPager) view.findViewById(R.id.view_pager);

		// tab
		mag = (MagicIndicator) view.findViewById(R.id.mag);

		mPagerAdapter = new TabPagerAdapter(getFragmentManager());

		// 设置适配器
		mViewPager.setAdapter(mPagerAdapter);

		// 初始顶部导航栏的方法
		initMag();

		Log.i("test",
				"当前保存的页面位置是 :"
						+ SPUtils.getInt(getActivity(),
								ConfigConstant.indecaterNum, 0));

		// 设置需要显示的当前页面位置
		mViewPager.setCurrentItem(SPUtils.getInt(getActivity(),
				ConfigConstant.indecaterNum, 0));
	}

	/**
	 * 初始化顶部Tab栏目的方法
	 */
	private void initMag() {
		IndicatorManager.setIndicator(getActivity(), 0, mag, mViewPager,
				mTitles, getResources().getColor(R.color.white),
				getResources().getColor(R.color.white), null);
	}

	@Override
	public String getFragmentName() {
		return TAG;
	}

	/**
	 * ViewPager的适配器
	 * 
	 * @author Administrator
	 *
	 */
	private class TabPagerAdapter extends FragmentStatePagerAdapter {

		public TabPagerAdapter(FragmentManager fm) {
			super(fm);
		}

		@Override
		public Fragment getItem(int position) {

			return mFragments.get(position);
		}

		@Override
		public int getCount() {
			return mFragments.size();
		}
	}

}
